package mp3;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import main.Driver;
import main.MusicFile;
import main.MusicType;
import main.Transitions;


 

 
public  class MP3ConversionManager implements MP3ConversionListenener{

	 public static MP3ConversionManager mp3ConversionManager;
 
 
	
	volatile private   HashMap<MusicFile, Integer> activeConversions = new HashMap<MusicFile, Integer>();
	 
	static {
		mp3ConversionManager = new MP3ConversionManager();
	}
  
	public   void convertToMp3(MusicFile fileToConvert) throws IOException {
		fileToConvert.transition = Transitions.WAVE_MP3;
		
		 MP3TOWAVCovertererImpl conver =new MP3TOWAVCovertererImpl(
				 fileToConvert.referenceFile.getAbsolutePath(),Driver.getLocalFolderPath()+ "\\" + fileToConvert.name +".mp3"
				 , fileToConvert, this);
		 
		 System.out.println("MP# connv");
		 activeConversions.put(fileToConvert, 0);
		 conver.start();
		 
	}
	
	
	public   int getPercentageComplete(MusicFile f) {
		System.out.println(f);
		
		if(!activeConversions.containsKey(f))
			return 0;
		
		return activeConversions.get(f);
	 
	}


	@Override
	public void completed(MusicFile m) {
		System.out.println("conversionLis");
		m.referenceFile.delete();
		m.referenceFile = new File(Driver.getLocalFolderPath() + "\\" + m.name + ".mp3");
		m.setPath(m.referenceFile.getParent());
		m.transition = Transitions.NONE;
		m.setMusicType(MusicType.MP3);
		
	}


	@Override
	public void transferred(MusicFile m, int percentageCompleted) {
		System.out.println(percentageCompleted);
		 Driver.updateUI();
		activeConversions.put(m, percentageCompleted);
	}


	@Override
	public void failed(MusicFile m) {
		// TODO Auto-generated method stub
		
	}
 
 

}
